package com.mvc.controller;

import com.alibaba.fastjson.JSON;
import com.mvc.model.UserInfo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
public class TestController {

    @RequestMapping("/test")
    public void test(String name,String address){

        System.out.println(name);
        System.out.println(address);
    }

    @RequestMapping("/test1")
    public void test1(HttpServletRequest request){

        System.out.println(request.getParameter("name"));
        System.out.println(request.getParameter("address"));
    }

    //第三种通过表单的name属性值获取参数
    //@RequestMapping(value = "/test2")
    @PostMapping(value = "test2")
    public void test2(String username,String address,String youxian){
        System.out.println(username);
        System.out.println(address);
        System.out.println(youxian);
    }

    @RequestMapping(value = "test3", method = RequestMethod.POST)
    public void test3(UserInfo userInfo){
        System.out.println(userInfo.toString());
    }

    @RequestMapping(value = "tojson", produces = "text/json;charset=UTF-8")
    //@RequestMapping(value = "tojson")
    public String toJson(){
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("我是中国人");
        userInfo.setYouxian("hexin1788@163.com");
        userInfo.setAddress("广东省广州市天河区");

       return JSON.toJSONString(userInfo);
    }

    @RequestMapping(value = "/tojsonjihe",produces = "text/json;charset=utf-8")
    public String toJsonjihe(){
        List<UserInfo> list = new ArrayList<>();
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("我是中国人");
        userInfo.setYouxian("hexin1788@163.com");
        userInfo.setAddress("广东省广州市天河区");

        UserInfo userInfo1 = new UserInfo();
        userInfo1.setUsername("我是中国人1");
        userInfo1.setYouxian("hexin1788@163.com1");
        userInfo1.setAddress("广东省广州市天河区1");

        UserInfo userInfo2 = new UserInfo();
        userInfo2.setUsername("我是中国人");
        userInfo2.setYouxian("hexin1788@163.com");
        userInfo2.setAddress("广东省广州市天河区");

        UserInfo userInfo3 = new UserInfo();
        userInfo3.setUsername("我是中国人");
        userInfo3.setYouxian("hexin1788@163.com");
        userInfo3.setAddress("广东省广州市天河区");

        list.add(userInfo);
        list.add(userInfo1);
        list.add(userInfo2);
        list.add(userInfo3);
        return JSON.toJSONString(list);
    }
}
